//
//  QLMBXOrderListVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/10/11.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMBXOrderListVC.h"
#import "QLMBXOrderListTabVCell.h"
#import "QLMBXOrderDetailVC.h"
#import "QLMOrderListArrModel.h"

@interface QLMBXOrderListVC ()<UITableViewDelegate,UITableViewDataSource>{
    QLMOrderListArrModel *orderListArrModel;
    QLMCustomTipView *tipView;
    
    NSInteger currentPage;
}
@property (nonatomic,strong) UITableView *otableview;
@end

@implementation QLMBXOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"报修订单列表";
    self.view.backgroundColor=[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    
    currentPage=1;
    orderListArrModel=[[QLMOrderListArrModel alloc] init];
    
    orderListArrModel.orders=[NSMutableArray array];
    
    CGRect rect=CGRectMake(0, 0, kScreen_width, kScreen_height-QLM_StatusBarAndNavigationBarHeight);
    _otableview=[[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    _otableview.backgroundColor=[UIColor clearColor];
    _otableview.delegate=self;
    _otableview.dataSource=self;
    _otableview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _otableview.showsVerticalScrollIndicator=NO;
    _otableview.showsHorizontalScrollIndicator=NO;
    _otableview.tableFooterView=[UIView new];
    [self.view addSubview:_otableview];
    
    //上拉刷新
    _otableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestOrderListData)];
    _otableview.mj_footer.automaticallyHidden = YES;
    
    tipView=[QLMCustomTipView sharedInstance];
    tipView.hidden = YES;
    [_otableview addSubview:tipView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    [self requestOrderListData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [orderListArrModel.orders removeAllObjects];
    currentPage=1;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)requestOrderListData{
    
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:kLoginToken];
    if ([token length]==0) {
        return;
    }
    NSDictionary *tokenDic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token", nil];
    NSMutableDictionary *paramerDic=[NSMutableDictionary dictionary];
    if (self.equipmentIdStr.length==0) {
        [paramerDic setObject:@"" forKey:@"equipmentId"];
    }else{
        [paramerDic setObject:self.equipmentIdStr forKey:@"equipmentId"];
    }
    
    [paramerDic setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
    [paramerDic setObject:@"10" forKey:@"rows"];
    
    [self.view makeToastActivity:CSToastPositionCenter];
    [ClientTool postUrlPath:@"/test/repairOrder/getOrderList" withParamers:paramerDic andBody:tokenDic
                    success:^(id responseObject){
                        
                        [self.view hideToastActivity];
                        [_otableview.mj_footer endRefreshing];
                        
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {
                        
                            [orderListArrModel.orders addObjectsFromArray:responseObject[@"orders"]];
    
                            if (orderListArrModel.orders.count==0) {
                                tipView.hidden = NO;
                            }else{
                                if ([responseObject[@"orders"] count]<10) {
                                    _otableview.mj_footer.state = MJRefreshStateNoMoreData;
                                }
                                tipView.hidden = YES;
                                currentPage++;
                            }
                            [_otableview reloadData];
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                        
                    }failure:^(NSError *error){
                        [self.view hideToastActivity];
                        
                        [_otableview.mj_footer endRefreshing];
                        ETToast(@"服务器连接失败");
                    }
     ];
}

- (NSString *)resultString:(NSString *)classificationId{
    NSString *classificationStr=nil;
    switch ([classificationId integerValue]) {
        case 0:
            classificationStr=@"发动机";
            break;
        case 1:
            classificationStr=@"电气";
            break;
        case 2:
            classificationStr=@"液压";
            break;
        case 3:
            classificationStr=@"其他";
            break;
        case 4:
            classificationStr=@"保养";
            break;
        default:
            break;
    }
    return classificationStr;
}

- (NSString *)resultStatus:(QLMOrderListInfoModel *)resultInfoModel{
    NSString *orderStatusStr=nil;
    switch ([resultInfoModel.orderStatus integerValue]) {
        case 0:
            orderStatusStr=@"待派工";
            break;
        case 1:
            orderStatusStr=@"已派工";
            break;
        case 2:
            orderStatusStr=@"已预约";
            break;
        case 3:
            orderStatusStr=@"已出发";
            break;
        case 4:
            orderStatusStr=@"已到达";
            break;
        case 5:
            if ([resultInfoModel.erpOrderStatus integerValue]==0) {
                orderStatusStr=@"待支付(服务结束)";
            }else if ([resultInfoModel.erpOrderStatus integerValue]==1){
                orderStatusStr=@"支付中";
            }else if ([resultInfoModel.erpOrderStatus integerValue]==2){
                orderStatusStr=@"已评价";
                if ([resultInfoModel.isEvaluation integerValue]==0) {
                    orderStatusStr=@"待评价";
                }
            }else if ([resultInfoModel.erpOrderStatus integerValue]==3){
                orderStatusStr=@"支付失败";
            }else{
                orderStatusStr=@"已结束";
            }
            break;
        case 6:
            orderStatusStr=@"已中断";
            break;
        case 7:
            orderStatusStr=@"中断出发";
            break;
        default:
            break;
    }
    return orderStatusStr;
}

#pragma mark - UITableView datasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView=[[UIView alloc] init];
    headerView.backgroundColor=[UIColor clearColor];
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return orderListArrModel.orders.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 210;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QLMBXOrderListTabVCell *cell=[QLMBXOrderListTabVCell cellWithTableView:tableView];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    QLMOrderListInfoModel *orderInfoModel=[[QLMOrderListInfoModel alloc] initWithDictionary:orderListArrModel.orders[indexPath.section] error:nil];
    
    QLMequipManagerModel *equipManagerModel=orderInfoModel.equipmentManager;
    
    cell.carnameLabel.text=[NSString stringWithFormat:@"设备：%@%@",equipManagerModel.brandName,equipManagerModel.modelName];
    
    if ([equipManagerModel.serialNo length]==0) {
        cell.carNumLabel.text=[NSString stringWithFormat:@"机号：无"];
    }else{
        cell.carNumLabel.text=[NSString stringWithFormat:@"机号：%@",equipManagerModel.serialNo];
    }
    
    if ([equipManagerModel.alias length]==0) {
        cell.otherNameLab.text=[NSString stringWithFormat:@"别名：无"];
    }else{
        cell.otherNameLab.text=[NSString stringWithFormat:@"别名：%@",equipManagerModel.alias];
    }
    
    cell.statusLabel.text=[NSString stringWithFormat:@"状态：%@",[self resultStatus:orderInfoModel]];
    cell.statusLabel.textColor=kNavigationBarColor;
    
    cell.troubleTypeLabel.text=[NSString stringWithFormat:@"故障类型：%@",[self resultString:orderInfoModel.classificationId]];
    cell.orderNoLab.text=[NSString stringWithFormat:@"订单编号：%@",orderInfoModel.erpOrderNo];
    cell.timeLabel.text=[NSString stringWithFormat:@"报修日期：%@",orderInfoModel.createTime];
    
    return cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QLMOrderListInfoModel *orderInfoModel=[[QLMOrderListInfoModel alloc] initWithDictionary:orderListArrModel.orders[indexPath.section] error:nil];
    
    QLMBXOrderDetailVC *bxOrderDetailVC=[QLMBXOrderDetailVC new];
    bxOrderDetailVC.repairOrderId=orderInfoModel.repairOrderId;
    bxOrderDetailVC.resultInfoModel=orderInfoModel;
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:bxOrderDetailVC animated:YES];
}

@end
